<template>
  <el-dialog
    :title="!dataForm.rentOid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="租房id" prop="rentId">
      <el-input v-model="dataForm.rentId" placeholder="租房id"></el-input>
    </el-form-item>
    <el-form-item label="用户手机号" prop="userPhone">
      <el-input v-model="dataForm.userPhone" placeholder="用户手机号"></el-input>
    </el-form-item>
    <el-form-item label="房屋id" prop="houseId">
      <el-input v-model="dataForm.houseId" placeholder="房屋id"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="房屋图片" prop="houseImg">
      <el-input v-model="dataForm.houseImg" placeholder="房屋图片"></el-input>
    </el-form-item>
    <el-form-item label="租期" prop="leaseTrem">
      <el-input v-model="dataForm.leaseTrem" placeholder="租期"></el-input>
    </el-form-item>
    <el-form-item label="租赁价格" prop="housePrice">
      <el-input v-model="dataForm.housePrice" placeholder="租赁价格"></el-input>
    </el-form-item>
    <el-form-item label="订单创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="订单创建时间"></el-input>
    </el-form-item>
    <el-form-item label="支付价格" prop="orderPrice">
      <el-input v-model="dataForm.orderPrice" placeholder="支付价格"></el-input>
    </el-form-item>
    <el-form-item label="订单状态" prop="orderStatus">
      <el-input v-model="dataForm.orderStatus" placeholder="订单状态"></el-input>
    </el-form-item>
    <el-form-item label="支付时间" prop="payTime">
      <el-input v-model="dataForm.payTime" placeholder="支付时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          rentOid: 0,
          rentId: '',
          userPhone: '',
          houseId: '',
          userId: '',
          houseImg: '',
          leaseTrem: '',
          housePrice: '',
          createTime: '',
          orderPrice: '',
          orderStatus: '',
          payTime: ''
        },
        dataRule: {
          rentId: [
            { required: true, message: '租房id不能为空', trigger: 'blur' }
          ],
          userPhone: [
            { required: true, message: '用户手机号不能为空', trigger: 'blur' }
          ],
          houseId: [
            { required: true, message: '房屋id不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          houseImg: [
            { required: true, message: '房屋图片不能为空', trigger: 'blur' }
          ],
          leaseTrem: [
            { required: true, message: '租期不能为空', trigger: 'blur' }
          ],
          housePrice: [
            { required: true, message: '租赁价格不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '订单创建时间不能为空', trigger: 'blur' }
          ],
          orderPrice: [
            { required: true, message: '支付价格不能为空', trigger: 'blur' }
          ],
          orderStatus: [
            { required: true, message: '订单状态不能为空', trigger: 'blur' }
          ],
          payTime: [
            { required: true, message: '支付时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.rentOid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.rentOid) {
            this.$http({
              url: this.$http.adornUrl(`/user/houserentorder/info/${this.dataForm.rentOid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.rentId = data.houseRentOrder.rentId
                this.dataForm.userPhone = data.houseRentOrder.userPhone
                this.dataForm.houseId = data.houseRentOrder.houseId
                this.dataForm.userId = data.houseRentOrder.userId
                this.dataForm.houseImg = data.houseRentOrder.houseImg
                this.dataForm.leaseTrem = data.houseRentOrder.leaseTrem
                this.dataForm.housePrice = data.houseRentOrder.housePrice
                this.dataForm.createTime = data.houseRentOrder.createTime
                this.dataForm.orderPrice = data.houseRentOrder.orderPrice
                this.dataForm.orderStatus = data.houseRentOrder.orderStatus
                this.dataForm.payTime = data.houseRentOrder.payTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/houserentorder/${!this.dataForm.rentOid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'rentOid': this.dataForm.rentOid || undefined,
                'rentId': this.dataForm.rentId,
                'userPhone': this.dataForm.userPhone,
                'houseId': this.dataForm.houseId,
                'userId': this.dataForm.userId,
                'houseImg': this.dataForm.houseImg,
                'leaseTrem': this.dataForm.leaseTrem,
                'housePrice': this.dataForm.housePrice,
                'createTime': this.dataForm.createTime,
                'orderPrice': this.dataForm.orderPrice,
                'orderStatus': this.dataForm.orderStatus,
                'payTime': this.dataForm.payTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
